package com.linjian.togetherbirthday.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.gson.Gson;
import com.linjian.togetherbirthday.R;
import com.linjian.togetherbirthday.activity.SplashActivity;
import com.linjian.togetherbirthday.base.BaseFragment;
import com.linjian.togetherbirthday.view.DatePickerView;
import com.sdsmdg.tastytoast.TastyToast;

import java.util.ArrayList;
import java.util.Map;

public class HeightSetFragment extends BaseFragment implements DatePickerView.onSelectListener, View.OnClickListener {

    private View mView;
    private DatePickerView height_dpv;
    private Button bt_go;

    private String userHeight = null;

    private SplashActivity activity;

    private ArrayList<String> heightList;

    public static HeightSetFragment newInstance(String info) {
        HeightSetFragment heightSetFragment = new HeightSetFragment();
        Bundle bundle = new Bundle();
        bundle.putString("info",info);
        heightSetFragment.setArguments(bundle);
        return heightSetFragment;
    }

    @Override
    protected int setView() {
        return R.layout.fragmnet_height;
    }

    @Override
    protected void init(View view) {
        mView = view;
        activity = (SplashActivity) getActivity();
        height_dpv = view.findViewById(R.id.height_pv);
        bt_go = view.findViewById(R.id.bt_go_height);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        initHeight();
        setListener();
    }
    private void setListener() {
        height_dpv.setOnSelectListener(this);
        bt_go.setOnClickListener(this);
    }
    private void initHeight(){
        if (heightList == null){
            heightList = new ArrayList<>();
        }
        heightList.clear();
        for (int i = 130; i < 200 ; i++) {
            heightList.add(String.valueOf(i));
        }
        height_dpv.setData(heightList);
        height_dpv.setSelected(35);
        height_dpv.setCanScroll(heightList.size() > 1);
    }

    @Override
    public void onSelect(String text) {
        userHeight = text;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_go_height:
                if (userHeight == null){
                    TastyToast.makeText(getActivity(),"请先选择身高",TastyToast.LENGTH_SHORT,TastyToast.WARNING).show();
                }else {
                    Map<String,Object> map = new Gson().fromJson(getArguments().getString("info"),Map.class);
                    map.put("height",Integer.valueOf(userHeight));
                    activity.replaceFragment("NickUserPicFragment",new Gson().toJson(map));
                }
                break;
        }
    }
}
